package com.aaa.hospital.ManagementEnd.controller;

import com.aaa.hospital.Client.pojo.Visitingcard;
import com.aaa.hospital.ManagementEnd.pojo.ExpenseCalendar;
import com.aaa.hospital.ManagementEnd.pojo.User;
import com.aaa.hospital.ManagementEnd.service.ExpenCalendService;
import com.aaa.hospital.ManagementEnd.service.MedChargeSerevice;
import com.aaa.hospital.ManagementEnd.service.UserServices;

import com.aaa.hospital.ManagementEnd.util.PayDB;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserServices userService;
    @Autowired
    private ExpenCalendService expenCalendService;
    @Autowired
    private MedChargeSerevice medChargeSerevice;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public String queryAll(){
        JSONObject json = new JSONObject();
        List<User> data = userService.queryAll();;
        Object objData=JSONObject.toJSON(data);
        json.put("code", 0);
        json.put("msg", "");
        json.put("count",data.size());
        json.put("data",objData);
        return json.toJSONString();
    }
   @RequestMapping("/user/listById/{uid}")
       public User  listById(@PathParam("uid") Integer uid){

        return userService.user_ById(uid);

       }


    @RequestMapping(value = "/user_insert",method = RequestMethod.GET)
    @ResponseBody
    public boolean  user_insert(User user){

        return userService.user_insert(user);

    }

    @RequestMapping("/user_update")
    @ResponseBody
    public Boolean  user_update(User user){

        return userService.user_update(user);
    }

    @RequestMapping("/user_del")
    @ResponseBody
    public Boolean  user_del(@PathParam("uid") Integer uid){

        return userService.user_del(uid);
    }




//搜索框查询
@RequestMapping("/limitAll")
@ResponseBody
    public String limitAll(@Param("uid") Integer uid,@Param("uname") String uname){
    JSONObject json = new JSONObject();
    List<User> users = userService.limitAll(uid, uname);
    Object objData=JSONObject.toJSON(users);
    json.put("code", 0);
    json.put("msg", "");
    json.put("count",users.size());
    json.put("data",objData);
    return json.toJSONString();
    }
    //cyx
    @RequestMapping("/insertCard")
    @ResponseBody
    public Boolean insertCard(User user,@PathParam("balance")Integer balance){
        Visitingcard result = userService.queryByIdCard(user.getUname(), user.getIdcard());
        if(result!=null){
            return false;
        }else {
            Boolean aBoolean = userService.insertCard(user);
            return aBoolean;
        }
    }
    @RequestMapping("/selectAll")
    @ResponseBody
    public String selectAll(){
        JSONObject json = new JSONObject();
        List<PayDB> users =userService.selectAll();
        Object objData=JSONObject.toJSON(users);
        json.put("code", 0);
        json.put("msg", "");
        json.put("count",users.size());
        json.put("data",objData);
        return json.toJSONString();
    }

    @RequestMapping("/queryByIdCardVisid")
    @ResponseBody
    public String queryByIdCardVisid(@PathParam("visid")Integer visid,@PathParam("idcard")String idcard){
        JSONObject json = new JSONObject();
        List<PayDB> payDBS = userService.queryByIdCardVisid(visid, idcard);
        Object objData=JSONObject.toJSON(payDBS);
        json.put("code", 0);
        json.put("msg", "");
        json.put("count",payDBS.size());
        json.put("data",objData);
        return json.toJSONString();
    }


    @RequestMapping("/queryUserAll")
    @ResponseBody
    public User queryUserAll(@PathParam("visid")Integer visid){
        System.out.println(visid);
        User user = userService.queryUserAll(visid);
       
        return user;
    }

    @RequestMapping("/updateBalance")
    @ResponseBody
    public Boolean updateBalance(String uname,String idcard,Integer price,String charge){
        Visitingcard visid = userService.queryByIdCard(uname, idcard);






        if(visid!=null){
            boolean byvisid = medChargeSerevice.Byvisid(visid.getVisid(),price);
            if(byvisid==true){
              return expenCalendService.ecInsert(new ExpenseCalendar(null,visid.getVisid(),null,charge,price));
            }else {
                return false;
            }
        }else {
            return false;
        }
    }

}
